package DBTransaction;

import Util.JdbcUtil;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * Created by sunwei on 2017/9/28
 * Time:10:30
 */
public final class TransactionHelper {
    private static String userName;//表示定义的数据库的用户名
    private static String password;//表示定义的数据库的密码
    private static String driver; //定义的数据库的驱动信息
    private static String URL; //定义访问数据的地址
    private Connection connection;//定义数据库的链接
    private PreparedStatement pstmt;//定义sql语句的执行对象
    private ResultSet resultSet;//定义查询返回结果的结果集
    //使用ThreadLocal持有当前线程的数据库连接
    private final static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();

    /*
    连接配置，来自connection.properties
     */
    private final static Properties properties = new Properties();

    static{
        TransactionHelper.loadConfig();
    }
    /*
    加载配置文件
     */
    public static void loadConfig(){
        try {
            InputStream inStream = JdbcUtil.class.getResourceAsStream("/connection.properties");
            Properties prop = new Properties();
            prop.load(inStream);
            userName = prop.getProperty("jdbc.username");
            password = prop.getProperty("jdbc.password");
            driver = prop.getProperty("jdbc.driver");
            URL= prop.getProperty("jdbc.url");
        } catch (Exception e) {
            throw new RuntimeException("读取数据库配置文件异常",e);
        }
    }

    /*
    获取当前线程中的数据库连接
    */
    private static Connection getCurrentConnection()
    {
        Connection conn = threadLocal.get();
        if(conn == null){
            conn =  createNotAutoCommitConnection();
            threadLocal.set(conn);
        }
        return conn;
    }

    /*
    执行SQL语句
    */
    public static int executeNonQuery(String sql) throws SQLException {
        //获取连接
        Connection conn = getCurrentConnection();
        //调用executeUpdate()方法，执行增、删、改操作
        return conn.createStatement().executeUpdate(sql);

    }

    /*
    提交事务
    */
    public static void commit(){
        Connection conn = getCurrentConnection();
        try {
            conn.commit();
            conn.close();
            threadLocal.set(null);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage(),e);
        }
    }


    /*
    回滚事务
    */
    public static void rollback(){
        Connection conn = getCurrentConnection();
        try {
            conn.rollback();
            conn.close();
            threadLocal.set(null);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage(),e);
        }
    }

    /*
    创建一个不自动Commit的数据库连接
    */
    private static Connection createNotAutoCommitConnection() {
        try {

            Connection conn = DriverManager.getConnection(URL,userName,password);
            conn.setAutoCommit(false);
            return conn;
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage(),e);
        }
    }
}

